package com.stone.persistent.recyclerview.helper;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.Nullable;
import androidx.viewpager2.widget.ViewPager2;


public class CarouselHelper extends Handler {
    private ViewPager2 viewPager2;
    private static final long LOOP_INTERVAL = 3000L;

    public void handleMessage(@Nullable Message msg) {
        int nextItem = this.viewPager2.getCurrentItem() + 1;
        this.viewPager2.setCurrentItem(nextItem);
        this.sendEmptyMessageDelayed(1, LOOP_INTERVAL);
    }

    public final void start() {
        this.removeCallbacksAndMessages( null);
        this.sendEmptyMessageDelayed(1, LOOP_INTERVAL);
    }

    public final void stop() {
        this.removeCallbacksAndMessages( null);
    }

    public CarouselHelper( ViewPager2 viewPager2) {
        super(Looper.getMainLooper());
        this.viewPager2 = viewPager2;
    }


}
